package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.service.GoodsService;
import com.ctgu.fastleader.utility.ExcelImport;
import com.ctgu.fastleader.vo.Goods;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    GoodsService goodsService;

//    初始化表格
    @ResponseBody
    @RequestMapping("/allGoods")
    public List<Goods> allGoods(){
        List<Goods> goodsList = goodsService.findAllGoods();
        System.out.println(goodsList);
        return goodsList;
    }

    //模糊查询
    @ResponseBody
    @RequestMapping("/searchGoodsByKeyWord")
    public List<Goods> searchGoodsByKeyWord(Integer goodsid, String goodsname){

        List<Goods> goodsList = goodsService.searchGoodsByKeyWord(goodsid,goodsname);
        return goodsList;
    }

    //预警查询
    @ResponseBody
    @RequestMapping("/selectGoodsNumLow")
    public int selectGoodsNumLow(){
        System.out.println(goodsService.selectGoodsNumLow());
        return goodsService.selectGoodsNumLow();
    }

    //新增商品
    @RequestMapping("/addGoods")
    public String addGoods(HttpServletRequest request){
        Integer goodsid = Integer.valueOf(request.getParameter("goodsid"));
        String goodsname = request.getParameter("goodsname");
        Double goodsprice = Double.valueOf(request.getParameter("goodsprice"));
        String goodsinfo = request.getParameter("goodsinfo");
        Goods goods = new Goods(goodsid,goodsname,goodsprice,goodsinfo,0);
        goodsService.insertSelective(goods);
        return "redirect:/page/toProduct";
    }

    //删除商品通过id数组
    @ResponseBody
    @RequestMapping("/deleteGoods")
    public int deleteOutputOrder(HttpServletRequest request,String[] obj){
        int[] id = new int[obj.length];
        for (String array:obj){
            goodsService.deleteByPrimaryKey(Integer.parseInt(array));
        }
        return 0;
    }

    //补货
    @RequestMapping("/addInputorder")
    public String addInputorder(Integer goodsid, Model model){
        model.addAttribute("goodsid",goodsid);
        return "forward:/page/toInputOrderAdd";
    }

//    导入
    @RequestMapping("/importGoods")
    public String importGoods(MultipartFile file) {
        if(!file.isEmpty()){
            String filename = "1.xlsx";
            String path = "D:\\";
            File file1 = new File(path+filename);
            try {
                file.transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        ExcelImport excelImport = new ExcelImport();
        try {
            List<Map> list = excelImport.importExcelAction("D:\\1.xlsx");
            for(Map map:list){
                Double id = Double.parseDouble((String) map.get("id"));
                int temp = (new Double(id)).intValue();
                Integer goodsid = new Integer(temp);
                String goodsname = (String) map.get("name");
                Double goodsprice = Double.parseDouble((String) map.get("price"));
                String goodsinfo = (String) map.get("info");
                Goods goods = new Goods(goodsid,goodsname,goodsprice,goodsinfo,0);
                goodsService.insertSelective(goods);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "forward:/page/toProduct";
    }
}
